package com.itzhigui.goods.controller;

import com.itzhigui.common.entity.Goods;
import com.itzhigui.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 商品相关接口
 *
 * @author java1412
 * @since 2023-10-29 17:39:41
 */
@RestController
@RequestMapping("/api/v1/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Value("${server.port}")
    private String port;

    @RequestMapping("/findByGoodsId")
    public Object findByGoodsId(int goodsId) {
        System.out.println("端口号 : " + port);
        Goods goods = goodsService.findById(goodsId);
        return goods;
    }

    // 测试负载均衡机制
    @RequestMapping(value = "/findClusterName")
    public Object findClusterName(HttpServletRequest request) {
        //request.getServerName():获取服务名称
        // request.getServerPort()：获取端口号
        String clusterName = "当前服务器名称: " + request.getServerName() + ";当前集群节点端口号:"
                + request.getServerPort();
        return clusterName;
    }
}
